// packageStore/pages/purchase/dealing/dealing.js
import {util,api, storage} from '../../../../js/util';
import { router } from '../../../../js/router';
import {dealing} from '../../../../js/modules/market';
var app = getApp();
import BasePage from '../../../../js/common/base-page'
import { config } from "../../../../js/modules/gloab.js";
BasePage({
  pageKey:"dealing",
  /**
   * 页面的初始数据
   */
  data: {
    isIphoneX:app.globalData.isIphoneX,
    mainColor:api.titleColor||"#e56b14",//主题色
    Properties:{},
    dataList:[],
    OddNumber:'',
    OrderType:0,
    BeginTime:'',
    EndTime:'',
    PageIndex: 1,
    PageSize: 20,
    currentDate:'',
    type:['全部','订单','预存款','退款单'],
    Currency:'',//货币符号
    orderTypeList: ['线上', '全部'],
    selectedOrderTypeIndex: 0,
    quantityDigits: 2, // 数量显示小数位数
    priceDigits: 2, // 价格显示小数位数
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let current = new Date();
    let year = current.getFullYear(),
        month = current.getMonth()+1,
        day = current.getDate();

    this.setData({
      currentDate:year+'-'+month+'-'+day
    })
    this.getConfig()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.loadDealing();
    this.setData({
      Currency:api.Currency || '￥'
    })
  },

  /**
   * 加载往来账数据
   */
  loadDealing:function(){
    let that = this;
    let {OddNumber,OrderType,BeginTime,EndTime,PageIndex,PageSize} = that.data;
    OrderType = OrderType - 1
    if (OrderType == -1) {
      OrderType = ''
    }
    let param = {
      OddNumber:OddNumber,
      OrderType:OrderType,
      BeginTime:BeginTime,
      EndTime:EndTime,
      PageIndex:PageIndex,
      PageSize:PageSize
    }
    dealing.getDealing(param,function(res){
      console.log(res);
      that.setData({
        Properties:res.properties
      })
      var dataListTem = that.data.dataList;
      if (PageIndex == 1) {
        dataListTem = [];
      }
      if (res.count > 0) {
        var contentlist = res.data;
        if (contentlist.length < that.data.PageSize) {
          that.setData({
            dataList: dataListTem.concat(contentlist),
            hasMoreData: false
          });
        } else {
          that.setData({
            dataList: dataListTem.concat(contentlist),
            hasMoreData: true,
            PageIndex: PageIndex + 1
          });
        }

      } else {
        that.setData({
          dataList: [],
          hasMoreData: true,
          PageIndex: 1
        });
      }
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.setData({
      PageIndex:1
    },function(){
      this.loadDealing();
    })
    wx.stopPullDownRefresh();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (this.data.hasMoreData) {
      this.loadDealing();
    }
  },


  showOrderTypeList() {
    this.setData({
      showOrderType: true
    })
  },
  selectOrderType(e) {
    let index = e.currentTarget.dataset.index
    if (index == 1) {
      this.setData({
        showOrderType: false
      })
      storage.put('page_dealing_type', 'offline', 0)
      router.redirect({
        path: 'dealingOffline'
      })
    }
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  /**
   * 去付款
   */
  goPay:function(){
    router.navigate({
      path: 'arrears',
    })
  },
  /**
   * 打开筛选
   */
  showFilter:function(){
    let that = this;
    // that.resetFilter();   //重置筛选
    var animation = wx.createAnimation({
      duration: 200,
      timingFunction: "linear",
      delay: 0
    });
    //that.animation = animation
    animation.translateX('100%').step()
    that.setData({
      animationData: animation.export(),
      showModalStatus: true
    });
    setTimeout(function() {
      animation.translateX(0).step()
      that.setData({
        animationData: animation.export()
      })
    }.bind(that), 200);
  },
  /**
   * 关闭筛选
   */
  hideFilter:function(){
    let that = this;
    var animation = wx.createAnimation({
      duration: 200,
      timingFunction: "linear",
      delay: 0
    });
    //that.animation = animation
    animation.translateX(0).step()
    that.setData({
      animationData: animation.export(),
    });
    setTimeout(function() {
      animation.translateX('100%').step()
      that.setData({
        animationData: animation.export(),
        showModalStatus: false
      })
    }.bind(that), 200);
  },
  preventTouchMove:function() {

  },
  /**
   * 选择日期
   */
  bindDateChange: function(e) {
    let dateType = e.currentTarget.dataset.type
    this.setData({
      [dateType]: e.detail.value
    })
  },
  /**
   * 选择单据类型
   */
  bindTypeChange:function(e){
    this.setData({
      OrderType:e.detail.value
    })
  },
  /**
   * 绑定单据编号
   */
  bindCodeValue:function(e){
    this.setData({
      OddNumber:util.trim(e.detail.value)
    })
  },
  /**
   * 重置筛选
   */
  resetFilter:function(){
    this.setData({
      OrderType: 0,
      BeginTime:'',
      EndTime:'',
      OddNumber:''
    })
  },
  /**
   * 确定筛选
   */
  sureFilter:function(){
    this.setData({
      PageIndex:1
    },function(){
      this.loadDealing();
    })
    this.hideFilter();
  },
  toOrderDetail (e) {
    let item = e.currentTarget.dataset.item || {}
    if (item.Type == 1 || item.Type == 2) {
      return
    }
    router.navigate({
      path: 'orderDetail',
      params: {
        oddNumber: item.OddNumber
      }
    })
  },
  getConfig () {
    let that = this
    let fkid = app.dns.proprietorId
    let fkflag = app.dns.proprietor
    config.getWholesaleConfigs(
    fkid,
    fkflag,
    'Wholesale',
      (model)=> {
        that.setData({
          quantityDigits: model.QuantityDigits,
          priceDigits: model.PriceDigits
        })
    })
  },
})